home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / comint / telnet.el < prev    next >
Encoding:
Text File  |  1995-07-28  |  11.5 KB  |  297 lines

  1. ;;; telnet.el --- run a telnet session from within an Emacs buffer
  2.  
  3. ;;; Copyright (C) 1985, 1988, 1992, 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Author: William F. Schelter
  6. ;; Keywords: comm, unix
  7. ;; Maintainer: FSF
  8.  
  9. ;; This file is part of XEmacs.
  10.  
  11. ;; XEmacs is free software; you can redistribute it and/or modify it
  12. ;; under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; XEmacs is distributed in the hope that it will be useful, but
  17. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. ;; General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  23. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; This mode is intended to be used for telnet or rsh to a remode host;
  28. ;; `telnet' and `rsh' are the two entry points.  Multiple telnet or rsh
  29. ;; sessions are supported.
  30. ;;
  31. ;; Normally, input is sent to the remote telnet/rsh line-by-line, as you
  32. ;; type RET or LFD.  C-c C-c sends a C-c to the remote immediately; 
  33. ;; C-c C-z sends C-z immediately.  C-c C-q followed by any character
  34. ;; sends that character immediately.
  35. ;;
  36. ;; All RET characters are filtered out of the output coming back from the
  37. ;; remote system.  The mode tries to do other useful translations based
  38. ;; on what it sees coming back from the other system before the password
  39. ;; query.  It knows about UNIX, ITS, TOPS-20 and Explorer systems.
  40.  
  41. ;;; Code:
  42.  
  43. ;; to do fix software types for lispm:
  44. ;; to eval current expression.  Also to try to send escape keys correctly.
  45. ;; essentially we'll want the rubout-handler off.
  46.  
  47. ;; filter is simplistic but should be okay for typical shell usage.
  48. ;; needs hacking if it is going to deal with asynchronous output in a sane
  49. ;; manner
  50.  
  51. (require 'comint)
  52.  
  53. (defvar telnet-mode-map nil)
  54. (defvar telnet-new-line "\r")
  55. (make-variable-buffer-local 'telnet-new-line)
  56. (defvar telnet-default-prompt-pattern "^[^#$%>]*[#$%>] *")
  57. (defvar telnet-prompt-pattern telnet-default-prompt-pattern)
  58. (defvar telnet-replace-c-g nil)
  59. (make-variable-buffer-local 'telnet-replace-c-g)
  60. (defvar telnet-remote-echoes t
  61.    "True if the telnet process will echo input.")
  62. (make-variable-buffer-local 'telnet-remote-echoes)
  63. (defvar telnet-interrupt-string "\C-c"
  64.   "String sent by C-c.")
  65. (make-variable-buffer-local 'telnet-interrupt-string)
  66.  
  67. (defvar telnet-count 0
  68.   "Number of output strings read from the telnet process
  69. while looking for the initial password.")
  70. (make-variable-buffer-local 'telnet-count)
  71.  
  72. (defvar telnet-initial-count -50
  73.   "Initial value of `telnet-count'.  Should be set to the negative of the
  74. number of terminal writes telnet will make setting up the host connection.")
  75.  
  76. (defvar telnet-maximum-count 4
  77.   "Maximum value `telnet-count' can have.
  78. After this many passes, we stop looking for initial setup data.
  79. Should be set to the number of terminal writes telnet will make
  80. rejecting one login and prompting for the again for a username and password.")
  81.  
  82. (defun telnet-interrupt-subjob ()
  83.   (interactive)
  84.   "Interrupt the program running through telnet on the remote host."
  85.   (process-send-string nil telnet-interrupt-string))
  86.  
  87. (defun telnet-c-z ()
  88.   (interactive)
  89.   (process-send-string nil "\C-z"))
  90.  
  91. (defun telnet-send-process-next-char ()
  92.   (interactive)
  93.   (process-send-string nil
  94.            (char-to-string
  95.         (let ((inhibit-quit t))
  96.           (prog1 (read-char)
  97.             (setq quit-flag nil))))))
  98.  
  99. ; initialization on first load.
  100. (if telnet-mode-map
  101.     nil
  102.   (progn
  103.     (setq telnet-mode-map (make-sparse-keymap))
  104.     (set-keymap-name telnet-mode-map 'telnet-mode-map)
  105.     (set-keymap-parent telnet-mode-map comint-mode-map)
  106.     (define-key telnet-mode-map "\C-m" 'telnet-send-input)
  107.     ;;(define-key telnet-mode-map "\C-j" 'telnet-send-input)
  108.     (define-key telnet-mode-map "\C-c\C-q" 'telnet-send-process-next-char)
  109.     (define-key telnet-mode-map "\C-c\C-c" 'telnet-interrupt-subjob) 
  110.     (define-key telnet-mode-map "\C-c\C-z" 'telnet-c-z)))
  111.  
  112. ;;maybe should have a flag for when have found type
  113. (defun telnet-check-software-type-initialize (string)
  114.   "Tries to put correct initializations in.  Needs work."
  115.   (let ((case-fold-search t))
  116.     (cond ((string-match "unix" string)
  117.        (setq telnet-prompt-pattern shell-prompt-pattern)
  118.        (setq telnet-new-line "\n"))
  119.       ((string-match "tops-20" string) ;;maybe add telnet-replace-c-g
  120.        (setq telnet-prompt-pattern  "[@>] *"))
  121.       ((string-match "its" string)
  122.        (setq telnet-prompt-pattern  "^[^*>]*[*>] *"))
  123.       ((string-match "explorer" string) ;;explorer telnet needs work
  124.        (setq telnet-replace-c-g ?\n))
  125.       (t
  126.        (setq telnet-prompt-pattern telnet-default-prompt-pattern))))
  127.   (setq comint-prompt-regexp telnet-prompt-pattern))
  128.  
  129. (defun telnet-initial-filter (proc string)
  130.   ;For reading up to and including password; also will get machine type.
  131.   (cond ((string-match "No such host" string)
  132.      (kill-buffer (process-buffer proc))
  133.      (error "No such host."))
  134.     ((string-match "passw" string)
  135.      (telnet-filter proc string)
  136.      (let ((password (comint-read-noecho "Password: ")))
  137.        (setq telnet-count 0)
  138.        (process-send-string proc (concat password telnet-new-line))))
  139.     (t (telnet-check-software-type-initialize string)
  140.        (telnet-filter proc string)
  141.        (cond ((> telnet-count telnet-maximum-count)
  142.           ;; (set-process-filter proc 'telnet-filter)
  143.           ;; Kludge for shell-fonts -- this is the only mode that
  144.           ;; actually changes what its process filter is at run time,
  145.           ;; which confuses shell-font.  So we special-case that here.
  146.           ;; #### Danger, knows an internal shell-font variable name.
  147.           (let ((old-filter (process-filter proc)))
  148.             (if (eq old-filter 'shell-font-process-filter)
  149.             (set (make-local-variable 'shell-font-process-filter)
  150.                  'telnet-filter)
  151.               (set-process-filter proc 'telnet-filter))))
  152.          (t (setq telnet-count (1+ telnet-count)))))))
  153.  
  154. ;; Identical to comint-simple-send, except that it sends telnet-new-line
  155. ;; instead of "\n".
  156. (defun telnet-simple-send (proc string)
  157.   (comint-send-string proc string)
  158.   (comint-send-string proc telnet-new-line))
  159.  
  160. (defun telnet-filter (proc string)
  161.   (save-excursion
  162.     (set-buffer (process-buffer proc))
  163.     (save-match-data
  164.      (let* ((last-insertion (marker-position (process-mark proc)))
  165.         (delta (- last-insertion (point)))
  166.         (ie (and comint-last-input-end
  167.              (marker-position comint-last-input-end)))
  168.         (w (get-buffer-window (current-buffer)))
  169.         (ws (and w (window-start w))))
  170.        (goto-char last-insertion)
  171.     ;; Insert STRING, omitting all C-m characters.
  172.        (insert-before-markers string)
  173.        (set-marker (process-mark proc) (point))
  174.        ;; the insert-before-markers may have screwed window-start
  175.        ;; and likely moved comint-last-input-end.  This is why the
  176.        ;; insertion-reaction should be a property of markers, not
  177.        ;; of the function which does the inserting.
  178.        (if ws (set-window-start w ws t))
  179.        (if ie (set-marker comint-last-input-end ie))
  180.        (while (search-backward "\C-m" last-insertion t)
  181.      (delete-char 1))
  182.        (goto-char (process-mark proc))
  183.        (and telnet-replace-c-g
  184.         (subst-char-in-region last-insertion (point) ?\C-g
  185.                   telnet-replace-c-g t))
  186.        (goto-char (+ (process-mark proc) delta))
  187.        ))))
  188.  
  189. (defun telnet-send-input ()
  190.   (interactive)
  191.   (let ((proc (get-buffer-process (current-buffer)))
  192.     p1 p2)
  193.     (if (and telnet-remote-echoes
  194.          (>= (point) (process-mark proc)))
  195.     (save-excursion
  196.       (if comint-eol-on-send (end-of-line))
  197.       (setq p1 (marker-position (process-mark proc))
  198.         p2 (point))))
  199.     (prog1
  200.     (comint-send-input)
  201.       ;; at this point, comint-send-input has moved the process mark, inserted
  202.       ;; a newline, and possibly inserted the (echoed) output.  If the host is
  203.       ;; in remote-echo mode, then delete our local copy of the command, and
  204.       ;; the newline that comint-send-input sent.
  205.       (if p1
  206.       (delete-region p1 (1+ p2))))))
  207.  
  208. ;;;###autoload
  209. (defun telnet (host &optional port)
  210.   "Open a network login connection to host named HOST (a string).
  211. With a prefix argument, prompts for the port name or number as well.
  212. Communication with HOST is recorded in a buffer *HOST-telnet*.
  213. Normally input is edited in Emacs and sent a line at a time.
  214. See also `\\[rsh]'."
  215.   (interactive (list (read-string "Open telnet connection to host: ")
  216.              (if current-prefix-arg
  217.              (read-string "Port name or number: ")
  218.                nil)))
  219.   (let* ((name (concat (if port (concat host "/" port) host) "-telnet"))
  220.          (buffer (get-buffer (concat "*" name "*"))))
  221.     (if (and buffer (get-buffer-process buffer))
  222.     (switch-to-buffer buffer)
  223.       (progn
  224.     (switch-to-buffer (make-comint name "telnet"))
  225.     (set-process-filter (get-process name) 'telnet-initial-filter)
  226.  
  227.     ;; SunOS and IRIX don't print "unix" in their rsh or telnet
  228.     ;; login banners, so let's get a reasonable default here.
  229.     ;; #### This patch from jwz mimics what is done in rsh done
  230.     ;; below.  However, it (along with the one in rsh) mean that
  231.     ;; telnet-check-software-type-initialize is effectively a
  232.     ;; wastoid function.  Reworking it like it claims to need is
  233.     ;; probably the better solution but I'm not going to do it.
  234.     ;; --cet
  235.     (telnet-check-software-type-initialize "unix")
  236.  
  237.     ;; Don't send the `open' cmd till telnet is ready for it.
  238.     (accept-process-output (get-process name))
  239.     (erase-buffer)
  240.     (process-send-string name (concat "open " host
  241.                       (if port (concat " " port) "")
  242.                       "\n"))
  243.     (setq comint-input-sender 'telnet-simple-send)
  244.     (setq telnet-count telnet-initial-count)
  245.     ;; run last so that hooks can change things.
  246.     (telnet-mode)))))
  247.  
  248. (defun telnet-mode ()
  249.   "This mode is for using telnet (or rsh) from a buffer to another host.
  250. It has most of the same commands as comint-mode.
  251. There is a variable ``telnet-interrupt-string'' which is the character
  252. sent to try to stop execution of a job on the remote host.
  253. Data is sent to the remote host when RET is typed.
  254.  
  255. \\{telnet-mode-map}
  256. "
  257.   (interactive)
  258.   (comint-mode)
  259.   (setq major-mode 'telnet-mode
  260.         mode-name "Telnet"
  261.         comint-prompt-regexp telnet-prompt-pattern)
  262.   (use-local-map telnet-mode-map)
  263.   (run-hooks 'telnet-mode-hook))
  264.  
  265. ;; Berkeley spawn of hell
  266. ;;;###autoload
  267. (defun rsh (host)
  268.   "Open a network login connection to host named HOST (a string).
  269. Communication with HOST is recorded in a buffer *HOST-rsh*.
  270. Normally input is edited in Emacs and sent a line at a time.
  271. See also `\\[telnet]'."
  272.   (interactive "sOpen rsh connection to host: ")
  273.   (require 'shell)
  274.   (let ((name (concat host "-rsh")))
  275.     (switch-to-buffer (make-comint name "rsh" nil host))
  276.     (setq telnet-count telnet-initial-count)
  277.     ;;
  278.     ;; SunOS doesn't print "unix" in its rsh login banner, so let's get a
  279.     ;; reasonable default here.  There do exist non-Unix machines which
  280.     ;; speak the rsh protocol, but let's hope they print their OS name
  281.     ;; when one connects.
  282.     ;;
  283.     (telnet-check-software-type-initialize "unix")
  284.     ;;
  285.     ;; I think we should use telnet-filter here instead of -initial-filter,
  286.     ;; because rsh generally doesn't prompt for a password, and gobbling the
  287.     ;; first line that contains "passw" is extremely antisocial.  More
  288.     ;; antisocial than echoing a password, and more likely than connecting
  289.     ;; to a non-Unix rsh host these days...
  290.     ;;
  291.     ;; (set-process-filter (get-process name) 'telnet-initial-filter)
  292.     (set-process-filter (get-process name) 'telnet-filter)
  293.     ;; run last so that hooks can change things.
  294.     (telnet-mode)))
  295.  
  296. (provide 'telnet)
  297.